package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.domain.PmsBrand;
import com.sky.domain.PmsCategoryBrandRelation;
import com.sky.mapper.PmsCategoryBrandRelationMapper;
import com.sky.service.PmsBrandService;
import com.sky.mapper.PmsBrandMapper;
import com.sky.utils.PageQuery;
import com.sky.utils.Query;
import com.sky.utils.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author Administrator
* @description 针对表【pms_brand(品牌)】的数据库操作Service实现
* @createDate 2024-06-30 16:46:42
*/
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements PmsBrandService{

    @Resource
    private PmsCategoryBrandRelationMapper pmsCategoryBrandRelationMapper;

    /**
     * @param param :
     * @return Page<PmsBrand>
     * @author zhl
     * @description 分页查询
     * @date 2024/7/4 15:21
     */
    @Override
    public IPage<PmsBrand> queryPage(PageQuery<PmsBrand> param) {
        LambdaQueryWrapper<PmsBrand> wrapper = new LambdaQueryWrapper<>();
        PmsBrand pmsBrand = param.getQuery();
        if (StringUtils.isNotEmpty(pmsBrand.getName())){
            wrapper.like(PmsBrand::getName, pmsBrand.getName());
        }
        IPage<PmsBrand> page = baseMapper.selectPage(new Query<PmsBrand>().getPage(param), wrapper);
        return page;
    }

    @Override
    public boolean updateBrand(PmsBrand pmsBrand) {
        baseMapper.updateById(pmsBrand);

        String brandName = pmsBrand.getName();
        if (StringUtils.isNotEmpty(brandName)){
            pmsCategoryBrandRelationMapper.update(null,new LambdaUpdateWrapper<PmsCategoryBrandRelation>()
                    .eq(PmsCategoryBrandRelation::getBrandId, pmsBrand.getBrandId())
                    .set(PmsCategoryBrandRelation::getBrandName, brandName));
        }

        return true;
    }
}




